﻿using Infrastructure;
using Lan.Dto;
using Lan.Model;
using Lan.Model.Vo;
using Lan.Repository;
using Lan.ServiceCore.IService;
using Lan.ServiceCore.TargetCollection;
using Model;
using SqlSugar;

namespace Lan.ServiceCore.Services
{
    [AppService(ServiceType = typeof(IDefenceareaService), ServiceLifetime = LifeTime.Transient)]
    public class DefenceareaService : Repository<DefenceareaModel>, IDefenceareaService
    {
        public DefenceareaModel InsertUser(DefenceareaModel sysUser)
        {
            Insertable(sysUser).ExecuteReturnIdentity();
            return sysUser;
        }

        private readonly IRadarService RadarService;
        private readonly ICameraService CameraService;
        public DefenceareaService(IRadarService radarService = null, ICameraService cameraService = null)
        {
            RadarService = radarService;
            CameraService = cameraService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public List<DefenceareaModel> GetList(DefenceareaQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var exp = Expressionable.Create<DefenceareaModel>();
            exp.AndIF(!string.IsNullOrEmpty(parm.name), u => u.Name.Contains(parm.name));

            var response = Queryable()
                .Where(exp.ToExpression()).ToList();

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public DefenceareaModel GetInfo(int Id)
        {
            var response = Queryable().Filter(null, true)
                .Where(f => f.Id == Id).First();
            if (response != null && response.Id > 0)
            {
                response.Cameras = CameraService.SelectCameraIds(Id);
                response.CameraIds = response.Cameras.Select(x => x.Id).ToArray();

                response.Radars = RadarService.SelectRadarIds(Id);
                response.RadarIds = response.Radars.Select(x => x.Id).ToArray();
            }
            return response;
        }

        public int AddDefencearea(DefenceareaModel model)
        {
            int id = Insertable(model).ExecuteReturnIdentity();
            CameraService.unUpdateBindCamera(id);
            RadarService.unUpdateBindRadar(id);

            CameraService.UpdateBindCamera(model, id);
            RadarService.UpdateBindRadar(model, id);
            return 1;
        }

        public int UpdateDefencearea(DefenceareaModel model)
        {
            CameraService.unUpdateBindCamera(model.Id);
            RadarService.unUpdateBindRadar(model.Id);

            CameraService.UpdateBindCamera(model, model.Id);
            RadarService.UpdateBindRadar(model, model.Id);

            return Update(model, true);
        }
        public int DeleteDefencearea(object id)
        {
            return Delete(id);
        }
        private static Expressionable<DefenceareaModel> QueryExp(DefenceareaQueryDto parm)
        {
            var predicate = Expressionable.Create<DefenceareaModel>();

            return predicate;
        }

        public List<DefenceareaModel> GetAllList()
        {
            return Queryable().ToList();
        }

        public List<TreeSelectVo> BuildDeptTreeSelect(List<DefenceareaDtoParent> depts)
        {
            List<DefenceareaDtoParent> menuTrees = BuildDeptTree(depts);
            List<TreeSelectVo> treeMenuVos = new List<TreeSelectVo>();
            foreach (var item in menuTrees)
            {
                treeMenuVos.Add(new TreeSelectVo(item));
            }
            return treeMenuVos;
        }

        public List<DefenceareaDtoParent> BuildDeptTree(List<DefenceareaDtoParent> depts)
        {
            List<DefenceareaDtoParent> returnList = new List<DefenceareaDtoParent>();
            List<long> tempList = depts.Select(f => f.Id).ToList();
            foreach (var dept in depts)
            {
                // 如果是顶级节点, 遍历该父节点的所有子节点
                if (!tempList.Contains(dept.ParentId))
                {
                    RecursionFn(depts, dept);
                    returnList.Add(dept);
                }
            }

            if (!returnList.Any())
            {
                returnList = depts;
            }
            return returnList;
        }
        /// <summary>
        /// 递归列表
        /// </summary>
        /// <param name="list"></param>
        /// <param name="t"></param>
        private void RecursionFn(List<DefenceareaDtoParent> list, DefenceareaDtoParent t)
        {
            //得到子节点列表
            List<DefenceareaDtoParent> childList = GetChildList(list, t);
            t.children = childList;
            foreach (var item in childList)
            {
                if (GetChildList(list, item).Count() > 0)
                {
                    RecursionFn(list, item);
                }
            }
        }
        /// <summary>
        /// 递归获取子菜单
        /// </summary>
        /// <param name="list">所有菜单</param>
        /// <param name="dept"></param>
        /// <returns></returns>
        private List<DefenceareaDtoParent> GetChildList(List<DefenceareaDtoParent> list, DefenceareaDtoParent dept)
        {
            return list.Where(p => p.ParentId == dept.Id).ToList();
        }
    }
}
